Linked list
Linked list
struct CList
{
int items[MAXLIST];
int last_index;
}list;
void main() break;
{ case 2:
int x; x=remove(&list);
int option; printf("\n The removed item is: %d", x);
char ch='y'; break;
list.last_index=-1; case 3:
printf("\n What do you want to do?"); display(&list);
printf("\n1.Add an item"); break;
printf("\n2.Delete an item"); case 4:
printf("\n3.Display List"); exit();
printf("\n4.Exit"); default:
while(ch=='y') printf("Invalid Option");
{ }
printf("\n Enter your option:"); printf("\n Do you want to continue(y/n)?");
scanf("%d", &option); scanf(" %c", &ch);
switch(option) }
{ getch();
case 1: }
printf("\n Enter item to insert:");
scanf("%d", &x);
insert(&list, x);
void insert(struct CList *list, int x) if(pos>(list->last_index)+1)
{ {
int pos; printf("\n Invalid");
int i; return;
if(list->last_index==MAXLIST-1) }
{ else
printf("\n List if Full."); {
return; i=list->last_index;
} while(i>=pos)
if(list->last_index==-1) {
{ list->items[i+1]=list->items[i];
++(list->last_index); i=i-1;
list->items[0]=x; }
return; list->items[pos]=x;
} (list->last_index)++;
return;
printf("\n Enter position to insert:"); }
scanf("%d", &pos); }
int remove(struct CList *list) else
{ {
int pos; x=list->items[pos];
int i; i=pos;
int x; while(i<list->last_index)
if(list->last_index==-1) {
{ list->items[i]=list->items[i+1];
printf("\n List is empty."); i=i+1;
return -1; }
} (list->last_index)--;
else return x;
{ }
printf("\n Enter position to delete:"); }
scanf("%d", &pos); }
• NULL
• 5• 1022 • 1011• 6 • 1033• 1022• 7 • NULL
struct dll
{
int data;
struct dll *next;
struct dll *prev;
};
struct cll *head;
head=NULL;
Add at begining
• Let *head be the pointer to the first node and *newnode be another pointer
of structure type
1. Start
2. Create a new node using malloc() function.
i.e newnode=(struct SLL*)malloc(size of(struct SLL));
3. Read the number say num
4. Assign num to data field
i.e newnode->data=num;
5. Check the head pointer. If it points to NULL then
Set: head pointer to newnode
i.e head=newnode;
Set: newnode->next=newnode->prev=NULL
6. otherwise:
Set: next pointer field of newnode to first node
i.e newnode->next=head
Set: head->prev=newnode
7. Set: head pointer to newnode
i.e head=newnode;
7. stop