[ create a new paste ] login | about

Link: http://codepad.org/nUA2iCk3    [ raw code | output | fork ]

C, pasted on Mar 17:
// Activity on Singly-Linkedlist

#include <stdlib.h>

typedef struct node{
  int data;
  struct node *next;
} SN;
SN *insert(SN *head, int val){ /// insert at tail-end
  SN *newnode=(SN*)malloc(sizeof(SN));
  SN *tail=NULL;

  if(newnode==NULL){
    printf("\nAllocation for newnode fails.");
    exit(1);
  }
  newnode->data=val;
  newnode->next=NULL;

  if(head==NULL)
    head=newnode;
  else{
    tail=head;
    while(tail->next!=NULL)
      tail=tail->next;
    tail->next=newnode;
  }
  return head;
}
SN *insert2(SN *head, int val){ /// insert at beginning
  SN *newnode=(SN*)malloc(sizeof(SN));

  if(newnode==NULL){
    printf("\nAllocation for newnode fails.");
    exit(1);
  }
  newnode->data=val;
  newnode->next=NULL;

  if(head==NULL)
    head=newnode;
  else{
    newnode->next=head;
    head=newnode;
  }
  return head;
}
SN *delete(SN *head, int loc){ /// delete node in a particular location
  int i=1;
  SN *scanner, *prev;

  if(head!=NULL && loc>0){
    scanner=head;
    if(loc==i){
      head=scanner->next;
      free(scanner);
    }
    else{
      while(scanner->next!=NULL){
        prev=scanner;
        i++;
        scanner=scanner->next;
        if(loc==i)
          break;
      }
      if(loc==i){
        prev->next=scanner->next;
        free(scanner);
      }
    }
  }
  return head;
}
void display(SN *head){ /// displays all node values
  while(head!=NULL){
    printf("\n%d",head->data);
    head=head->next;
  }
}
int hasData(SN *head, int val){ /// returns 1/0 for True/False if val is in the list (pointed by head)
  while(head!=NULL){
    if(head->data==val)
      return 1;
    head=head->next;
  }
  return 0;
}
int findLocation(SN *head, int val){ /// returns the location of the val in the list starting at 1, otherwise 0 if it does not exists
  int i=0;
  while(head!=NULL){
    i++;
    if(head->data==val)
      return i;
    head=head->next;
  }
  return 0;
}
int update(SN *head, int oldval, int newval){ /// updates the oldval of node to newval
  int ucnt=0;
  while(head!=NULL){
    if(head->data==oldval){
      head->data=newval;
      ucnt++;
    }
    head=head->next;
  }
  /// returns the number of updates done a list
  return ucnt;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "Singly-LinkedList.c"
#define MAX 30

int top=-1;
int stack[MAX];

void push(char);
char pop();
int match(char a,char b);
int check(char []);

int main()
{
        SN *List=NULL;
        FILE *fn;
        fn=fopen("balanced.txt","r");
        if(fn==NULL){
        return 1;
}
	char exp[MAX];
	int valid;
	valid=check(exp);
	if(valid==1)
        return 1;
	else
		return 0;
}
int check(char exp[] )
{
	int i;
	char temp;
	for(i=0;i<strlen(exp);i++)
	{
		if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
			push(exp[i]);
		if(exp[i]==')' || exp[i]=='}' || exp[i]==']')
			if(top==-1)
			{
				return 0;
			}
			else
			{
				temp=pop();
				if(!match(temp, exp[i]))
				{
					printf("%c and %c\n",temp,exp[i]);
					return 0;
				}
			}
	}
	if(top==-1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
int match(char a,char b)
{
	if(a=='[' && b==']')
		return 1;
	if(a=='{' && b=='}')
		return 1;
	if(a=='(' && b==')')
		return 1;
	return 0;
}
void push(char item)
{
	if(top==(MAX-1))
	{
		return;
	}
	top=top+1;
	stack[top]=item;
}

char pop()
{
	if(top==-1)
	{
		exit(1);
	}
	return(stack[top--]);
}


Output:
1
Line 30: error: Singly-LinkedList.c: No such file or directory


Create a new paste based on this one


Comments: